Skip to content

Commit 7b31453

Browse files
TouyamaRiegithub-actions[bot]jasonren0403
authored
[zh-CN] add JavaScript.Reference.Errors.BigInt_division_by_zero (#27344)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jason Ren <40999116+jasonren0403@users.noreply.github.com>
1 parent d4d8dbd commit 7b31453

File tree

1 file changed

+51
-0
lines changed
  • files/zh-cn/web/javascript/reference/errors/bigint_division_by_zero

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: "RangeError: BigInt division by zero"
3+
slug: Web/JavaScript/Reference/Errors/BigInt_division_by_zero
4+
l10n:
5+
sourceCommit: c6f0f106b9083984dbf597678def6561729bb459
6+
---
7+
8+
{{jsSidebar("Errors")}}
9+
10+
当一个 {{jsxref("BigInt")}} 被 `0n` 除时,会产生 JavaScript 异常“BigInt division by zero”。
11+
12+
## 错误信息
13+
14+
```plain
15+
RangeError: Division by zero (V8-based)
16+
RangeError: BigInt division by zero (Firefox)
17+
RangeError: 0 is an invalid divisor value (Safari)
18+
```
19+
20+
## 错误类型
21+
22+
{{jsxref("RangeError")}}
23+
24+
## 哪里出错了?
25+
26+
当使用[除法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Division)或者[取余](/zh-CN/docs/Web/JavaScript/Reference/Operators/Remainder)运算符时,如果除数为 `0n` 则会触发该错误。在 {{jsxref("Number")}} 运算中,除以 `0n` 会得到 [`Infinity`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Infinity),但在 BigInt 中不存在“Infinity”这一值,因此会抛出错误。在使用除法前,请先检查除数是否为 `0n`
27+
28+
## 示例
29+
30+
### 被 0n 除
31+
32+
```js example-bad
33+
const a = 1n;
34+
const b = 0n;
35+
const quotient = a / b;
36+
// RangeError: BigInt division by zero
37+
```
38+
39+
相反,应首先检查除数是否为 `0n`,并给出更友好的提示或者使用其他值,例如 `Infinity``undefined`
40+
41+
```js example-good
42+
const a = 1n;
43+
const b = 0n;
44+
const quotient = b === 0n ? undefined : a / b;
45+
```
46+
47+
## 参见
48+
49+
- [`BigInt`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
50+
- [除法(`/`](/zh-CN/docs/Web/JavaScript/Reference/Operators/Division)
51+
- [取余(`%`](/zh-CN/docs/Web/JavaScript/Reference/Operators/Remainder)

0 commit comments

Comments
 (0)